//
//  EJSMineViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/13.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSMineViewController.h"
#import "EJSSettingTableViewController.h"
#import "EJSMyAccountViewController.h"
#import "EJSHelpCenterViewController.h"
#import "EJSFeedbackViewController.h"
#import "EJSCouponViewController.h"
#import "EJSUserInfoViewController.h"
#import "EJSUpgradeVipViewController.h"
#import "EJSInviteFriendViewController.h"
#import "EJSOrderListViewController.h"

#import <UIView+BlocksKit.h>
#import "EJSShareView.h"
#import "EJSMineHeaderView.h"
#import "EJSMineTableViewCell.h"
#import "EJSMineToolHeaderView.h"

#import "EJSUserInfoRequest.h"
#import "EJSMineToolModel.h"
#import "EJSAboutUsViewController.h"
#import "EJSUserInfoViewController.h"
#import "EJSManageAddressViewController.h"
#import <SDWebImageManager.h>
#import <SDImageCache.h>
#import "EJSMineInfoTableViewCell.h"
#import "EJSMineToolTableViewCell.h"

#import "MSGInputInviteCodeViewController.h"

@interface EJSMineViewController ()<EJSMineTableViewCellDelegate, EJSMineToolTableViewCellDelegate>{
}
@property (strong, nonatomic) EJSMineHeaderView *headerView;
@property (strong, nonatomic) EJSUserModel *userModel;

@end

@implementation EJSMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataSource = [[EJSMineToolListModel getDefaultToolList] mutableCopy];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    self.userModel = [EJSUserModel userModel];
    if([EJSUserTokenModel userTokenModel]){
        [self refreshUserInfo];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    self.headerView = [EJSMineHeaderView mineHeaderView];
    CGFloat statusHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
    CGFloat naviHeight = NavigationBarHeight;
    CGFloat headerViewHeight = naviHeight + 150;
    self.headerView.frame = CGRectMake(0, 0, ScreenWidth, headerViewHeight);
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.contentInset = UIEdgeInsetsMake(statusHeight, 0, 0, 0);
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView refreshUserInfo];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSMineTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSMineTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSMineToolHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"EJSMineToolHeaderView"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EJSMineToolTableViewCell" bundle:nil] forCellReuseIdentifier:@"EJSMineToolTableViewCell"];
    
    UIView *tableBottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 100)];
    tableBottomView.backgroundColor = kClearColor;
    UIButton *logoutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [logoutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [logoutBtn setTitleColor:UIColorFromRGB(0x6C6363) forState:UIControlStateNormal];
    logoutBtn.titleLabel.font = kPingFangFontRegularSize(14);
    logoutBtn.frame = CGRectMake(0, 50, ScreenWidth, 50);
    [logoutBtn addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    [tableBottomView addSubview:logoutBtn];
    self.tableView.tableFooterView = tableBottomView;
    
    FlyWeakSelf
    [self.headerView.userInfoView bk_whenTapped:^{
        if([weakSelf userIsLogin]){
            EJSUserInfoViewController *aboutUsVC = [EJSUserInfoViewController viewController];
            [weakSelf.navigationController pushViewController:aboutUsVC animated:YES];
        }
    }];
    
    [self.headerView.loginView bk_whenTapped:^{
        if([weakSelf userIsLogin]){
            EJSUserInfoViewController *aboutUsVC = [EJSUserInfoViewController viewController];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
        }
    }];
    
    self.headerView.vipVIew.hidden = YES;
    
    [self.headerView.vipBtn addTarget:self action:@selector(vipAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)refreshUserInfo{
    EJSUserInfoRequest *req = [EJSUserInfoRequest new];
    req.hideLoadingView = YES;
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.userModel = response.data;
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)setUserModel:(EJSUserModel *)userModel{
    _userModel = userModel;
    [self.headerView refreshUserInfo];
}


#pragma mark - Actions
- (void)vipAction:(UIButton *)sender{
    if([self userIsLogin]){
        EJSUpgradeVipViewController *upgradVipVC = [[EJSUpgradeVipViewController alloc] initWithNibName:@"EJSUpgradeVipViewController" bundle:nil];
        [self.navigationController pushViewController:upgradVipVC animated:YES];
    }
}

- (void)logoutAction:(UIButton *)sender {
    [EJSUserModel logout];
    self.userModel = [EJSUserModel userModel];
    if(![EJSUserTokenModel userTokenModel]){
        [WHToast showMessage:@"退出登录成功" duration:1 finishHandler:nil];
        self.tableView.contentOffset = CGPointZero;
        
        //TODO: 清除购物车信息
        
    }
}

#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        EJSMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSMineTableViewCell" forIndexPath:indexPath];
           cell.toolLisModel = [self.dataSource objectAtIndex:indexPath.section];
           cell.delegate = self;
           return cell;
    }else {
        EJSMineToolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EJSMineToolTableViewCell" forIndexPath:indexPath];
        EJSMineToolListModel *tools = [self.dataSource objectAtIndex:indexPath.section];
        cell.listModel = tools;
        cell.delegate = self;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 110;
    }
    EJSMineToolListModel *tools = [self.dataSource objectAtIndex:indexPath.section];
    return tools.items.count*50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UITableViewHeaderFooterView *headerView = [[UITableViewHeaderFooterView alloc] init];
    headerView.contentView.backgroundColor = UIColorFromRGB(0xf3f4f6);
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UITableViewHeaderFooterView *footerView = [[UITableViewHeaderFooterView alloc] init];
    footerView.contentView.backgroundColor = UIColorFromRGB(0xf3f4f6);
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)mineToolTableViewDidClickTool:(NSIndexPath *)indexPath {
    if(![self userIsLogin]) return;
    UIViewController *vc;
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0: // perfil
                vc = [EJSUserInfoViewController viewController];
                break;
            case 1:
               vc = [EJSManageAddressViewController viewController];
                break;
            default:
                break;
        }
    }else if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0:
                vc = [[EJSCouponViewController alloc] init];
                break;
            case 1:
//               vc = [[EJSInviteFriendViewController alloc] initWithNibName:@"EJSInviteFriendViewController" bundle:nil];
                //手动填入邀请码
                vc = [[MSGInputInviteCodeViewController alloc]initWithNibName:@"MSGInputInviteCodeViewController" bundle:nil];
                break;
            default:
                break;
        }
    }else if (indexPath.section == 2) {
        switch (indexPath.row) {
            case 0:
                vc = [[EJSFeedbackViewController alloc] initWithNibName:@"EJSFeedbackViewController" bundle:nil];
                break;
            case 1:
                [self showHudProgressClearCache];
                return;
            case 2:
               vc = [EJSHelpCenterViewController viewController];
                break;
            default:
               vc = [[EJSAboutUsViewController alloc] initWithNibName:@"FlyWebViewController" bundle:nil];
                break;
        }
    }
    [self.navigationController pushViewController:vc animated:YES];
}

- (void) showHudProgressClearCache {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                   [[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
                       [hud hideAnimated:YES];
                       [[HUDHelper sharedInstance] tipMessage:NSLocalizedString(@"清除成功", nil)];
                   }];
}


#pragma mark - EJSMineTableViewCellDelegate
- (void)tableViewCell:(UITableViewCell *)cell didSelectCollectionViewIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *tableViewIndexPath = [self.tableView indexPathForCell:cell];
    if(tableViewIndexPath.section == 0){
        if([self userIsLogin]){
            if(indexPath.item == 3){
                EJSOrderListViewController *orderVC = [[EJSOrderListViewController alloc] init];
//                orderVC.refoundFlag = @"refound";
                orderVC.orderType = @"4";
                [self.navigationController pushViewController:orderVC animated:YES];
            }
            else{
                EJSOrderListViewController *orderListVC = [[EJSOrderListViewController alloc] init];
                if(indexPath.item == 0){
                    orderListVC.orderType = @"1";
                }else if(indexPath.item == 1){
                    orderListVC.orderType = @"2";
                }else if(indexPath.item == 2){
                    orderListVC.orderType = @"3";
                }else if(indexPath.item == 4){
                    orderListVC.orderType = nil;
                }
                [self.navigationController pushViewController:orderListVC animated:YES];
            }
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}


@end
